/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.uca.sisbec.actions.reportes;

import com.uca.sisbec.commons.HibernateAction;
import com.uca.sisbec.forms.reportes.ReporteGastosProysForm;
import com.uca.sisbec.helpers.reportes.ReporteGastosProysHelper;
import com.uca.sisbec.mapping.GastosDeProyectos;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.*;

/**
 *
 * @author Alex
 */
public class ReporteGastosProysAction extends HibernateAction {
    public ActionForward init(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReporteGastosProysForm rgpf = (ReporteGastosProysForm) form;
        rgpf.reset(mapping, request);
        return mapping.findForward("success");
    }
    
    public ActionForward ConstruirReporteDeGastosProys(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) throws Exception {
        ReporteGastosProysForm rgpf = (ReporteGastosProysForm) form;
        ActionMessages msg = new ActionMessages();
        ActionErrors errors = new ActionErrors();
        
        if(rgpf.getFechaIni().trim().equals("") || rgpf.getFechaFin().trim().equals("")){
            errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("fechas.vacias"));
            request.setAttribute("errors", errors);
        } else {
            SimpleDateFormat formatoDeFecha = new SimpleDateFormat("yyyy-mm-dd");
            Date fechaIni = formatoDeFecha.parse(rgpf.getFechaIni());
            Date fechaFin = formatoDeFecha.parse(rgpf.getFechaFin());

            if (fechaFin.before(fechaIni) == true) {
                errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("intervalfecha.invalido"));
                request.setAttribute("errors", errors);
            } else {
                ReporteGastosProysHelper rgph = new ReporteGastosProysHelper(getSession(request));
                List<GastosDeProyectos> reporte = rgph.ConstruirReporteGastosProys(rgpf);
                msg.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("reporte.creado"));
                request.setAttribute("msg", msg);
                request.setAttribute("reporte", reporte);
            }
        }
        request.setAttribute("extra", rgpf.getExtraordinario());
        return mapping.findForward("success");
    }
}
